home *** CD-ROM | disk | FTP | other *** search
- /* ==========================================================================================
-
- C FILE: PackageRoutines.c
-
- COPYRIGHT: (c) 1992-94 Apple Computer Inc.
- All rights reserved.
-
- PURPOSE: This file contains the routines to package data to be sent to
- the plotter. These routines send the necessary HPGL commands
- to the plotter to achieve the desired output.
-
- HISTORY: Date By Description
- ----- --- --------------------------------------
- 1/20/92 Harita Initial release
- 12/20/93 dmh Sync'd up for GX 1.0b3.
- 8/28/94 dmh Sync'd up for GX 1.0.1.
- 6/14/96 cn Updated to support Universal Interfaces 2.1.
-
- ========================================================================================== */
-
- #include <stdio.h>
- #include <Types.h>
- #include <OSUtils.h>
- #include <Memory.h>
- #include <Errors.h>
- #include <ToolUtils.h>
- #include <FixMath.h>
-
- #include <GXTypes.h>
- #include <GXErrors.h>
- #include <GXGraphics.h>
- #include <GXMath.h>
-
- #include <GXPrinting.h>
- #include <GXPrinterDrivers.h>
-
-
- #include "ResourceDefines.h"
- #include "GlobalsDefs.h"
- #include "PackageRoutines.h"
-
- extern SpecDrvrGlobals gGlobals;
-
-
-
- /* =================================== INTERFACE ROUTINES =================================== */
-
- OSErr MyDumpBuffer(
- short n)
- {
- OSErr anErr = noErr;
-
- if ((n + gGlobals.bufferSize) >= kMaxBufferSize)
- {
- anErr = Send_GXBufferData(&gGlobals.buffer[0], gGlobals.bufferSize, gxNoBufferOptions);
- if (anErr != noErr)
- return(anErr);
- gGlobals.bufferSize = 0;
- }
- BlockMove(gGlobals.tempBuffer, &gGlobals.buffer[gGlobals.bufferSize], n);
- gGlobals.bufferSize += n;
-
-
- }
- /* ======= StartOfPagePlot =======
-
- StartOfPagePlot is called to send the HPGL commands to the device for beginning a new page.
- This routine issues a re-initialization command to the plotter.
- */
- OSErr StartOfPagePlot ( // (out) error code
- )
- {
- OSErr anErr = noErr;
- short n;
- Str255 theStr;
-
- GetIndString(theStr, kHPXLCmndStringsID, kInitIdx);
- theStr[theStr[0]+1] = 0;
- n = sprintf(gGlobals.tempBuffer, (char *) &theStr[1]);
- anErr = MyDumpBuffer(n);
- if (anErr != noErr)
- return(anErr);
-
- GetIndString(theStr, kHPXLCmndStringsID, kSetPenIdx);
- theStr[theStr[0]+1] = 0;
- n = sprintf(gGlobals.tempBuffer, (char *) &theStr[1], 1);
- anErr = MyDumpBuffer( n);
-
-
- return(anErr);
-
-
- } /* end of StartOfPagePlot */
-
-
- /* ======= EndOfPagePlot =======
-
- EndOfPagePlot is called to send the HPGL commands to the device for ending the page just
- plotted. This routine issues a "Return pen to carousel…" HPGL command.
- */
-
- OSErr EndOfPagePlot ( // (out) error code
- )
- {
- OSErr anErr = noErr;
- short n;
- Str255 theStr;
-
- GetIndString(theStr, kHPXLCmndStringsID, kSetPenIdx);
- theStr[theStr[0]+1] = 0;
- n = sprintf(gGlobals.tempBuffer, (char *) &theStr[1], 0);
- anErr = MyDumpBuffer(n);
-
- GetIndString(theStr, kHPXLCmndStringsID, kTermPageIdx);
- theStr[theStr[0]+1] = 0;
- n = sprintf(gGlobals.tempBuffer, (char *) &theStr[1]);
- anErr = MyDumpBuffer(n);
-
- anErr = Send_GXBufferData(&gGlobals.buffer[0], gGlobals.bufferSize, gxNoBufferOptions);
- if (anErr != noErr)
- return(anErr);
- gGlobals.bufferSize = 0;
-
- return(anErr);
-
-
- } /* EndOfPagePlot */
-
-
- /* ======= PlotLine =======
-
- PlotLine is called to send the HPGL2 commands to the device for plotting a line.
- The line to draw is specified by the lineToDraw shape.
- */
- OSErr PlotLine ( // (out) error code
- gxShape lineToDraw, // (in) Graphics shape describing the line to draw
- gxMapping theMapping) // (in) mapping use to scale points to device resolution
- {
- OSErr anErr = noErr;
- gxLine theLine;
- long startX;
- long startY;
- long endX;
- long endY;
- short n;
- Str255 theStr;
-
- GetIndString(theStr, kHPXLCmndStringsID, kLineIdx);
- theStr[theStr[0]+1] = 0;
-
-
- /* Get the points that compose the line */
- GXGetLine(lineToDraw, &theLine);
-
- /* Scale the points to the resolution of the device */
- MapPoints(&theMapping, 2, (gxPoint *) &theLine);
-
- startX = FixedToInt(theLine.first.x);
- startY = FixedToInt(theLine.first.y);
- endX = FixedToInt(theLine.last.x);
- endY = FixedToInt(theLine.last.y);
-
- /* Now package the data */
- n = sprintf(gGlobals.tempBuffer, (char *) &theStr[1], startX, startY, endX, endY);
- anErr = MyDumpBuffer(n);
-
-
- return(anErr);
- } /* PlotLine */
-
-
- /* ======= PlotRectangle =======
-
- PlotRectangle is called to send the HPGL commands to the device for plotting a non-filled
- rectangle. The rectangle to draw is specified by the rectToDraw shape.
- */
- OSErr PlotRectangle ( // (out) error code
- gxShape rectToDraw, // (in) Graphics shape describing the rectangle to draw
- gxMapping theMapping) // (in) mapping use to scale points to device resolution
- {
- OSErr anErr = noErr;
- gxRectangle theRectangle;
- long left;
- long top;
- long right;
- long bottom;
- gxShapeFill sFill;
- short n;
- Str255 theStr;
-
-
- /* Get the points that compose the line */
- GXGetRectangle(rectToDraw, &theRectangle);
-
- /* Scale the points to the resolution of the device */
- MapPoints(&theMapping, 2, (gxPoint *) &theRectangle);
-
- left = FixedToInt(theRectangle.left);
- top = FixedToInt(theRectangle.top);
- right = FixedToInt(theRectangle.right);
- bottom = FixedToInt(theRectangle.bottom);
-
- sFill = GXGetShapeFill(rectToDraw);
- if ((sFill == gxEvenOddFill ) || (sFill == gxWindingFill))
- GetIndString(theStr, kHPXLCmndStringsID, kFilledRectIdx);
- else
- GetIndString(theStr, kHPXLCmndStringsID, kRectIdx);
-
- theStr[theStr[0]+1] = 0;
- /* Now package the data */
- n = sprintf(gGlobals.tempBuffer, (char *) &theStr[1], left, top, right, bottom);
- anErr = MyDumpBuffer(n);
-
-
- return(anErr);
- } /* PlotRectangle */
-
-
- /* ======= PlotPolygon =======
-
- PlotPolygon is called to send the HPGL commands to the device for plotting a non-filled
- polygon. The polygon to draw is specified by the polyToDraw shape.
- */
- OSErr PlotPolygon ( // (out) error code
- gxShape polyToDraw, // (in) Graphics shape describing the polygon to draw
- gxMapping theMapping) // (in) mapping use to scale points to device resolution
- {
- OSErr anErr = noErr;
- gxPolygons *thePolys;
- long shapeSize;
- gxShapeFill sFill;
- long *p, contours, points, i;
- Boolean firstFlag;
- short startX, startY;
- short n;
- Str255 theStr;
-
- sFill = GXGetShapeFill(polyToDraw);
-
-
- /* Allocate enough space to hold the contents of the polygon shape */
-
- GXGetShapeStructure(polyToDraw, &shapeSize);
- thePolys = (gxPolygons *) NewPtr(shapeSize);
- if (thePolys == nil)
- {
- return(MemError());
- }
-
- /* Place the polygon geometry into the newly allocated pointer */
- GXGetPolygons(polyToDraw, thePolys);
- p = (long*) thePolys;
- contours = *p++;
- firstFlag = true;
-
- while (contours--)
- {
- GXJobIdle(); // give the forground app some time
- points = *p++;
- /* Scale the points to the resolution of the device */
- MapPoints(&theMapping, points, (gxPoint *) p);
-
- i = 0;
- startX = FixedToInt(*p++);
- startY = FixedToInt(*p++);
- if ((sFill != gxOpenFrameFill) && (firstFlag))
- GetIndString(theStr, kHPXLCmndStringsID, kInitPolyIdx);
- else
- GetIndString(theStr, kHPXLCmndStringsID, kInitLinesIdx);
-
- theStr[theStr[0]+1] = 0;
- /* Now package the data */
- n = sprintf(gGlobals.tempBuffer, (char *) &theStr[1], startX, startY);
- anErr = MyDumpBuffer(n);
- if (anErr != noErr) goto cleanup;
-
- for (i=1; i<points; ++i)
- {
- startX = FixedToInt(*p++);
- startY = FixedToInt(*p++);
- GetIndString(theStr, kHPXLCmndStringsID, kDataIdx);
- theStr[theStr[0]+1] = 0;
- n = sprintf(gGlobals.tempBuffer, (char *)&theStr[1], startX, startY);
- anErr = MyDumpBuffer(n);
- if (anErr != noErr) goto cleanup;
-
- if ((i+1) != points)
- {
- GetIndString(theStr, kHPXLCmndStringsID, kCommaIdx);
- theStr[theStr[0]+1] = 0;
- n = sprintf(gGlobals.tempBuffer, (char *) &theStr[1]);
- anErr = MyDumpBuffer(n);
- if (anErr != noErr) goto cleanup;
- }
- GXJobIdle(); // give foreground app some time
- }
-
- if (sFill == gxOpenFrameFill)
- GetIndString(theStr, kHPXLCmndStringsID, kTermLinesIdx);
- else if (sFill == gxClosedFrameFill)
- GetIndString(theStr, kHPXLCmndStringsID, kTermGlyphIdx);
- else
- {
- GetIndString(theStr, kHPXLCmndStringsID, kTermPolyIdx);
- firstFlag = false;
- }
- theStr[theStr[0]+1] = 0;
- n = sprintf(gGlobals.tempBuffer, (char *) &theStr[1]);
- anErr = MyDumpBuffer(n);
- if (anErr != noErr) goto cleanup;
- } // end of while loop
-
- if ((sFill != gxOpenFrameFill) && (sFill != gxClosedFrameFill))
- {
- GetIndString(theStr, kHPXLCmndStringsID, kTermPolysIdx);
- theStr[theStr[0]+1] = 0;
- n = sprintf(gGlobals.tempBuffer, (char *) &theStr[1]);
- anErr = MyDumpBuffer(n);
- if (anErr != noErr) goto cleanup;
-
- GetIndString(theStr, kHPXLCmndStringsID, kFillIdx);
- theStr[theStr[0]+1] = 0;
- n = sprintf(gGlobals.tempBuffer, (char *) &theStr[1]);
- anErr = MyDumpBuffer(n);
- if (anErr != noErr) goto cleanup;
- }
-
-
- cleanup:
- /* Deallocate the temporary pointer */
- DisposePtr((Ptr) thePolys);
-
-
- return(anErr);
- } /* PlotPolygon */
-